package com.mz361.common.utils;

import java.io.UnsupportedEncodingException;

/**
 * @author niceale
 * @date 2023/6/16
 */
public class BytesReadUtil {

    public BytesReadUtil() {

    }

    public static long bytes2Long(byte byteArray[], int x, int y) {
        long ti = 0;
        for (int i = (y - 1); i >= x; i--) {
            ti <<= 8;
            ti |= byteArray[i] & 0xff;
        }
        return ti;
    }

    public long toLong(byte byteArray[], int x, int y) {
        long ti = 0;
        for (int i = (y - 1); i >= x; i--) {
            ti <<= 8;
            ti |= byteArray[i] & 0xff;
        }
        return ti;
    }

    public static int bytes2Int(byte byteArray[], int x, int y) {
        int ti = 0;
        for (int i = (y - 1); i >= x; i--) {
            ti <<= 8;
            ti |= byteArray[i] & 0xff;
        }
        return ti;
    }

    public int toInt(byte byteArray[], int x, int y) {
        int ti = 0;
        for (int i = (y - 1); i >= x; i--) {
            ti <<= 8;
            ti |= byteArray[i] & 0xff;
        }
        return ti;
    }

    /**
     * helper method to convert 2 bytes to a short
     */
    public static short bytes2Short(byte byteArray[], int x, int y) {
        short ti = 0;
        for (int i = (y - 1); i >= x; i--) {
            ti <<= 8;
            ti |= byteArray[i] & 0xff;
        }
        return ti;
    }

    public short toShort(byte byteArray[], int x, int y) {
        short ti = 0;
        for (int i = (y - 1); i >= x; i--) {
            ti <<= 8;
            ti |= byteArray[i] & 0xff;
        }
        return ti;
    }

    public static String bytes2String(byte byteArray[], int offset, int length, String charsetName) {
        try {
            return new String(byteArray, offset, length, charsetName);
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        }
        return null;
    }

    public String toString(byte byteArray[], int offset, int length, String charsetName) {
        try {
            return new String(byteArray, offset, length, charsetName);
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        }
        return null;
    }

    public static byte[] int2Bytes(int number) {
        byte[] result = new byte[4];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) ((number >>> (8 * i)) & 0xff);
        }
        return result;
    }

    public byte[] toBytes(int number) {
        byte[] result = new byte[4];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) ((number >>> (8 * i)) & 0xff);
        }
        return result;
    }

    public static byte[] short2Bytes(short number) {
        byte[] result = new byte[2];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) ((number >>> (8 * i)) & 0xff);
        }
        return result;
    }

    public byte[] toBytes(short number) {
        byte[] result = new byte[2];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) ((number >>> (8 * i)) & 0xff);
        }
        return result;
    }

    public static byte[] long2Bytes(long number) {
        byte[] result = new byte[8];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) ((number >>> (8 * i)) & 0xff);
        }
        return result;
    }

    public byte[] toBytes(long number) {
        byte[] result = new byte[8];
        for (int i = 0; i < result.length; i++) {
            result[i] = (byte) ((number >>> (8 * i)) & 0xff);
        }
        return result;
    }

    public static byte[] String2Bytes(String str, String charsetName) {
        try {
            byte[] strBytes = null;
            strBytes = str.getBytes(charsetName);
            int len = strBytes.length;
            byte[] result = new byte[len + 1];
            result[0] = (byte) len;
            if (len > 0) {
                for (int i = 1; i < result.length; i++) {
                    result[i] = strBytes[i - 1];
                }
            }
            return result;
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        }
        return new byte[]{0};
    }

    public byte[] toBytes(String str, String charsetName) {
        try {
            byte[] strBytes = null;
            strBytes = str.getBytes(charsetName);
            int len = strBytes.length;
            byte[] result = new byte[len + 1];
            result[0] = (byte) len;
            if (len > 0) {
                for (int i = 1; i < result.length; i++) {
                    result[i] = strBytes[i - 1];
                }
            }
            return result;
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        }
        return new byte[]{0};
    }
}
